﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;

public class ResourceManager  {

    #region folder
    public const string rolesPrefabsPath = "Roles";
    public const string itemIconsPath = "Shared/UI/Sprites/ItemIcon";
    public const string otherIconsPath = "Shared/UI/Sprites/OtherIcon";
    public const string fontPath = "Font";
    public const string uiSoundPath = "Shared/UI/Sound";
    public const string battleSceneSoundPath = "Audios";
    public const string battleEffectPath = "Scene/Battle/Effects";
    public const string rankIconsPath = "Shared/UI/Sprites/RankIcons";
    public const string propIconsPath = "Icons/PropIcons";
    public const string goodsIconsPath = "Icons/GoodsIcons";
    public const string skinIconsPath = "Icons/SkinIcons";
    

    #endregion

    #region Battle

    public const string itemDirPath = "Scene/Battle/物件/道具";

    public const string battleUIPath = "Scene/Battle/UI/BattleUI/BattleUI";
    public const string debugViewPath = "Scene/Battle/UI/DebugView";
    public const string battleUIArrow = "Scene/Battle/UI/BattleUI/BattleUIArrow";
    public const string battleArrow = "Scene/Battle/物件/Arrow/Arrow";
    public const string battleTips = "Scene/Battle/UI/BattleTips/BattleTips";

    public const string hitFXCommon = battleEffectPath + "/tongyongbaodian";
    public const string itemFX = battleEffectPath + "/ItemFX/ItemFX";

    #endregion

    public const string tipsInfo = "Scene/Common/UI/TipsInfo.prefab";
    public const string scaleTips = "Scene/Common/UI/ScaleTips.prefab";
    
    #region common
    
    public const string sceneLoadingAnimPath = "Scene/Common/UI/NormalSceneLoadingAnim/NormalSceneLoadingAnim.prefab";
    public const string mainSceneUIChangeAnimPath = "Scene/Common/UI/MainSceneUIChangeAnim.prefab";
    public const string blackSceneLoadingAnimPath = "Scene/Common/UI/BlackSceneLoadingAnimPath.prefab";
    public const string audioSourceTemPath = "Shared/Prefabs/AudioSource.prefab";
    public const string fakeLoadingAnimPath = "";
    
    public const string roleShowScenePath = "Scene/Common/Prefabs/RoleShow";
    public const string rolesListLineScenePath = "Scene/Common/Prefabs/RolesListLineScene";
    public const string roleShowLockPath = "Scene/Common/Prefabs/RoleLocked.prefab";
    public const string guidePath = "Scene/Common/UI/Guide.prefab";

    #endregion

    
    
    #region audio
    
    public const string startSceneMusic = "Music/start_scene_music";
    public const string homeSceneMusic = "Music/pw_home_01";
    public const string battleSceneMusic = "Music/pw_battle_01";
    public const string resultWinMusic = "Music/pw_win";
    public const string resultFailMusic = "Music/pw_fail_e";

    #endregion

    public static T Load<T>(string resPath) where T : UnityEngine.Object
    {
        return Load<T>(resPath, false, false);
    }

    public static T Load<T>(string resPath, bool usePool, bool clear) where T : UnityEngine.Object
    {
        T res = null;

        if (usePool)
        {
            //  return EasyObjectPool.instance.GetObjectFromPool(assetPath, Vector3.zero, Quaternion.identity, clear);
        }
        else
        {
            if (resPath.Contains("Assets"))
            {
#if UNITY_EDITOR
                res = UnityEditor.AssetDatabase.LoadAssetAtPath<T>(resPath);
#endif
            }
            else
            {
                string extent = Path.GetExtension(resPath);
                string filePath = resPath;

                if (!string.IsNullOrEmpty(extent))
                    filePath = resPath.Replace(extent, "");

                res = Resources.Load<T>(filePath);
                if (res == null)
                    GameDebug.LogShowy("Load failed  filePath: " + filePath);
            }
        }
        return res;
    }

    public static T LoadPrefab<T>(string assetPath) where T : UnityEngine.Object
    {
        if (!assetPath.EndsWith(".prefab"))
        {
            assetPath = assetPath + ".prefab";
        }
        return Load<T>(assetPath, false, false);
    }

    static Sprite LoadSprite(string folderPath, string spirteName)
    {
        // string path = CommonFunctions.PathCombine(folderPath, spirteName);
        string path = Path.Combine(folderPath, spirteName);
        if (path.Contains("."))
            path = Path.GetFileNameWithoutExtension(path);

        path += ".png";
        // Debug.Log("LoadSprite  path:" + path);
        var go = Load<Sprite>(path);
        if (go != null)
        {
            return go;
        }
        else
        {
            GameDebug.LogShowy("LoadSprite failed! path:" + path);
            return null;
        }
    }
    public static AudioClip LoadUIAudio(string audioName)
    {
        return LoadAudio(uiSoundPath, audioName);
    }

    public static AudioClip LoadBattleSceneAudio(string audioName)
    {
        return LoadAudio(battleSceneSoundPath, audioName);
    }
    public static AudioClip LoadPetSceneAudio(int petId,string audioName)
    {
        return LoadAudio(rolesPrefabsPath+string.Format("/{0:D2}/Sound/", petId), audioName);
    }
    static AudioClip LoadAudio(string audiosDir, string audioName)
    {
        string path = Path.Combine(audiosDir, audioName);
        return LoadAudio(path);
    }

    public static AudioClip LoadAudio(string path)
    {
        //string path = CommonFunctions.PathCombine(audiosPath, audioName);
        if (path.Contains("."))
            path = Path.Combine( Path.GetDirectoryName(path), Path.GetFileNameWithoutExtension(path));

        path += ".wav";
        var go = Load<AudioClip>(path);

        //if (go == null)
        //{
        //    path = path.Replace(".wav", ".wma");
        //    go = Load<AudioClip>(path);
        //}
        //if (go == null)
        //{
        //    path = path.Replace(".wma", ".mp3");
        //    go = Load<AudioClip>(path);
        //}
        if (go != null)
        {
            return go;
        }
        else
        {
            GameDebug.LogShowy("LoadAudio failed! path:" + path);
            return null;
        }
    }


    #region 角色相关

    public static string GetRolePrefabPath(int roleId)
    {
        string prefabName = string.Format("{0:D2}", roleId);
        string relativePath = prefabName + "/" + prefabName;
        string path = Path.Combine(rolesPrefabsPath, relativePath);
        return path;
    }

    public static ZRoleAnim LoadRolePrefab(int roleId)
    {
        string path = GetRolePrefabPath(roleId);
        var prefab = LoadRolePrefab(path, false);
        return prefab;
    }

    public static ZRoleAnim LoadRolePrefab(string path, bool log)
    {
        if (!path.EndsWith(".prefab")) path += ".prefab";
        var go = Load<ZRoleAnim>(path);
        if (go != null)
        {
            return go;
        }
        else
        {
            if (log)
                Debug.LogError("LoadRolePrefab failed! path:" + path);
            return null;
        }
    }


    public static Sprite LoadRoleSkinIcon(int skinId)
    {
        string imgName = string.Format("{0:D2}", skinId);
        return LoadSprite(skinIconsPath, imgName);
    }
    
    #endregion

    public static Sprite LoadPropIcon(int propId)
    {
        string path = Path.Combine(propIconsPath,$"img_prop_{propId}");
        return Load<Sprite>(path);
    }

    public static Sprite LoadGoodsIcon(string goodsId)
    {
        string path = Path.Combine(goodsIconsPath, $"img_goods_{goodsId}");
        return Load<Sprite>(path);
    }
    
    public static Sprite LoadOtherIcon(string spriteName)
    {
        string path = Path.Combine(otherIconsPath, spriteName);
        return Load<Sprite>(path);
    }

    public static Sprite LoadRankIcon(int rank)
    {
        string spriteName = string.Format("img_rank_icon{0}", rank);
        string path = Path.Combine(rankIconsPath, spriteName);
        return Load<Sprite>(path);
    }
    
    
    public static Font LoadFont(string languageType)
    {
        string path = Path.Combine(fontPath, languageType.ToString());
        return Load<Font>(path);
    }
    public static TMP_FontAsset LoadTMPFont(string languageType)
    {
        string path = Path.Combine(fontPath,"TMP_"+ languageType.ToString());
        return Load<TMP_FontAsset>(path);
    }

    public static UnityEngine.GameObject LoadItem(int typeId)
    {
        string fileName = string.Format("Item{0:D2}", typeId);
        string relativePath = string.Format("{0}/{1}.prefab", fileName, fileName);
        string path = Path.Combine(itemDirPath, relativePath);
        return Load<UnityEngine.GameObject>(path);
    }

}
